/*****************************************************************************/
/*                    Perot Systems Framework                                */
/*****************************************************************************/
/* Stored Procedure :: ApplicationStartup()                                  */
/* Registers startup event for an application                                */
/*****************************************************************************/
/* Copyright (C) 2003-2004, Perot Systems Corporation. All right reserved.   */
/* Developer: Dima Frenkel (dima.frenkel@ps.net)                             */
/* Started:  08/04/2004                                                      */
/*****************************************************************************/
/* Modifications                                                             */
/* Date       |  Author          | Description                               */
/*------------+------------------+-------------------------------------------*/
/*            |                  |                                           */
/*****************************************************************************/

/******************************************************************************/
/* Step 0. Preparations/Declarations                                          */
/******************************************************************************/
SET NOCOUNT OFF
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
SET ARITHABORT ON
SET CONCAT_NULL_YIELDS_NULL ON
SET QUOTED_IDENTIFIER ON
SET NUMERIC_ROUNDABORT OFF
GO

/******************************************************************************/
/* Step 1. Remove stored procedure if it already exists                       */
/******************************************************************************/
IF (SELECT OBJECT_ID('[Kernel].[ApplicationStartup]')) IS NOT NULL BEGIN
  DROP PROCEDURE [Kernel].[ApplicationStartup] END

GO

/*****************************************************************************/
/* Stored Procedure :: ApplicationStartup                                    */
/*****************************************************************************/
/* Copyright (C) 2003-2004, Perot Systems Corporation. All right reserved.   */
/* Developer: Dima Frenkel (dima.frenkel@ps.net)                             */
/* Started:  08/04/2004                                                      */
/*****************************************************************************/
CREATE PROCEDURE [Kernel].[ApplicationStartup]( 
	@ApplicationName VARCHAR(128),
	@ApplicationVersion VARCHAR(32),
	@StartupParameters VARCHAR(128),
	@Host VARCHAR(64) = null,
	@StartTime datetime = null
)
AS 
BEGIN
    DECLARE @newOID BIGINT
    
    SET @ApplicationName    = ISNULL( @ApplicationName, APP_NAME() )
    SET @ApplicationVersion = ISNULL( @ApplicationVersion, 'UNKNOWN' )
    SET @StartupParameters  = ISNULL( @StartupParameters, '' )
    SET @Host               = ISNULL( @Host, HOST_NAME() )
    SET @StartTime          = ISNULL( @StartTime, GETDATE() )
    
    INSERT INTO ApplicationLog( ApplicationName, ApplicationVersion, StartupParameters, Host, StartTime, FinishTime  )
    VALUES( @ApplicationName,
            @ApplicationVersion,
            @StartupParameters,
            @Host,
            @StartTime,
            NULL )
    
    SET @newOID = SCOPE_IDENTITY()
    
    SELECT OID, [timestamp]
    FROM ApplicationLog WHERE OID = @newOID
END
GO

/******************************************************************************/
/* Step 3. Assign appropriate permissions                                     */
/******************************************************************************/
GRANT EXECUTE ON [Kernel].[ApplicationStartup] TO [Liberty_Users]
GO
-- Developers and Admins have permissions to execute through membership   
    